package com.ysj.aipicturebe.manager.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * WebSocket 配置类（定义连接）
 * * 该类实现了 WebSocketConfigurer 接口，用于注册 WebSocket 处理器和拦截器。
 * * 通过 @EnableWebSocket 注解启用 WebSocket 功能。
 * * 将前面我们定义的 PictureEditHandler websocket的处理器 和 WsHandshakeInterceptor websocket拦截器注入到配置类中，
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private PictureEditHandler pictureEditHandler;

    @Resource
    private WsHandshakeInterceptor wsHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(pictureEditHandler, "/ws/picture/edit")
                .addInterceptors(wsHandshakeInterceptor)
                .setAllowedOrigins("*");
    }
}
